package bar.dark.lee.widget;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.graphics.Color;
import android.view.View;
import android.view.View.OnClickListener;
import bar.dark.lee.BDConstents;
import bar.dark.lee.MainActivity;
import bar.dark.lee.R;
import bar.dark.lee.common.BDUtil;
import bar.dark.lee.common.DBSQLite;
import bar.dark.lee.common.DBSQLite.ParseSelectData;
import bar.dark.lee.manager.card.CreditCardResultDateData;
import bar.dark.lee.select.SelectActivity;
import bar.dark.lee.stats.StatsCategoryData;

public class WidgetGetSqlQuery
{
	private WidgetArrayList widgetList;
	
	public static final String TOTAL_MONEY = "0";
	public static final String BANK_MONEY = "1";
	public static final String REAL_MONEY = "2";
	public static final String CREDIT_MONEY = "3";
	public static final String MONTH_OUT_MONEY = "4";
	public static final String MONTH_POST_MONEY = "5";
	public static final String MONTH_IN_MONEY = "6";
	public static final String TODAY_OUT_MONEY = "7";
	public static final String TODAY_POST_MONEY = "8";
	public static final String TODAY_IN_MONEY = "9";
	public static final String BANKBOOK = "10";
	public static final String CHECKCARD = "11";
	public static final String CREDITCARD = "12";
	public static final String SECRET = "13";
	public static final String MONTH_MOVE_MONEY = "14";
	public static final String TODAY_MOVE_MONEY = "15";
	
	private Context context;
	
	private DBSQLite sql; 
	
	private BigDecimal money = BigDecimal.ZERO;
	
	private int toYear;
	private int toMonth;
	private int toDay;
	
	// 달 시작일 가져온다
	private int startDate;
	
	private String firstDate;
	private String lastDate;
	
	private ArrayList<CreditCardResultDateData> creditResultList = null;
	private ArrayList<CreditCardResultDateData> creditResultSelectList = null;
	
	private WidgetGetSqlQuery(){};
	
	/**
	 * 초기화 및 데이터 셋팅을 한다. 날짜 셋팅 시작일 셋팅 디비셋팅 등등.
	 * @param Context
	 */
	public WidgetGetSqlQuery(Context context)
	{
		this.context = context;
		this.sql = DBSQLite.getInstance(context);
		this.firstDate = "";
		this.lastDate = "";
		
		Calendar cal = Calendar.getInstance();
    	
    	toYear = cal.get(Calendar.YEAR);
    	toMonth = cal.get(Calendar.MONTH);
    	toDay = cal.get(Calendar.DAY_OF_MONTH);
    	
    	widgetList = new WidgetArrayList();
		
		setStartDate();
	}
	
	/**
	 * 큰 위젯 투명도 설정 값에 따른 레이아웃 아이디 값을 반환한다.
	 * @param kay
	 * @return
	 */
	public int getBigLayout(String key, int type)
	{
		int layoutId = 0;
		
		if (key.equals("0"))
		{
			if (type == 0)
			{
				layoutId = R.layout.accountbook_widget_big_0;
			}
			else
			{
				layoutId = R.layout.accountbook_widget_3x2_big_0;
			}
		}
		else if (key.equals("1"))
		{
			if (type == 0)
			{
				layoutId = R.layout.accountbook_widget_big_1;
			}
			else
			{
				layoutId = R.layout.accountbook_widget_3x2_big_1;
			}
		}
		else if (key.equals("2"))
		{
			if (type == 0)
			{
				layoutId = R.layout.accountbook_widget_big_2;
			}
			else
			{
				layoutId = R.layout.accountbook_widget_3x2_big_2;
			}
		}
		else if (key.equals("3"))
		{
			if (type == 0)
			{
				layoutId = R.layout.accountbook_widget_big_3;
			}
			else
			{
				layoutId = R.layout.accountbook_widget_3x2_big_3;
			}
		}
		else if (key.equals("4"))
		{
			if (type == 0)
			{
				layoutId = R.layout.accountbook_widget_big_4;
			}
			else
			{
				layoutId = R.layout.accountbook_widget_3x2_big_4;
			}
		}
		
		return layoutId;
	}
	
	/**
	 * 큰 위젯 투명도 값에 따른 레이아웃의 금액 텍스트 아이디를 반환한다.
	 * @param key
	 * @param number
	 * @return
	 */
	public int getBigMoneyId(String key, int number)
	{
		int layoutId = 0;
		
		if (key.equals("0"))
		{
			if (number == 1)
			{
				layoutId = R.id.widgetMoney1_widget_0;
			}
			else if (number == 2)
			{
				layoutId = R.id.widgetMoney2_widget_0;
			}
			else if (number == 3)
			{
				layoutId = R.id.widgetMoney3_widget_0;
			}
			else if (number == 4)
			{
				layoutId = R.id.widgetMoney4_widget_0;
			}
		}
		else if (key.equals("1"))
		{
			if (number == 1)
			{
				layoutId = R.id.widgetMoney1_widget_1;
			}
			else if (number == 2)
			{
				layoutId = R.id.widgetMoney2_widget_1;
			}
			else if (number == 3)
			{
				layoutId = R.id.widgetMoney3_widget_1;
			}
			else if (number == 4)
			{
				layoutId = R.id.widgetMoney4_widget_1;
			}
		}
		else if (key.equals("2"))
		{
			if (number == 1)
			{
				layoutId = R.id.widgetMoney1_widget_2;
			}
			else if (number == 2)
			{
				layoutId = R.id.widgetMoney2_widget_2;
			}
			else if (number == 3)
			{
				layoutId = R.id.widgetMoney3_widget_2;
			}
			else if (number == 4)
			{
				layoutId = R.id.widgetMoney4_widget_2;
			}
		}
		else if (key.equals("3"))
		{
			if (number == 1)
			{
				layoutId = R.id.widgetMoney1_widget_3;
			}
			else if (number == 2)
			{
				layoutId = R.id.widgetMoney2_widget_3;
			}
			else if (number == 3)
			{
				layoutId = R.id.widgetMoney3_widget_3;
			}
			else if (number == 4)
			{
				layoutId = R.id.widgetMoney4_widget_3;
			}
		}
		else if (key.equals("4"))
		{
			if (number == 1)
			{
				layoutId = R.id.widgetMoney1_widget_4;
			}
			else if (number == 2)
			{
				layoutId = R.id.widgetMoney2_widget_4;
			}
			else if (number == 3)
			{
				layoutId = R.id.widgetMoney3_widget_4;
			}
			else if (number == 4)
			{
				layoutId = R.id.widgetMoney4_widget_4;
			}
		}
		
		return layoutId;
	}
	
	/**
	 * 큰 위젯 투명도 값에 따른 레이아웃의 이름 텍스트 아이디를 반환한다.
	 * @param key
	 * @param number
	 * @return
	 */
	public int getBigTextViewId(String key, int number)
	{
		int layoutId = 0;
		
		if (key.equals("0"))
		{
			if (number == 1)
			{
				layoutId = R.id.widgetText1_widget_0;
			}
			else if (number == 2)
			{
				layoutId = R.id.widgetText2_widget_0;
			}
			else if (number == 3)
			{
				layoutId = R.id.widgetText3_widget_0;
			}
			else if (number == 4)
			{
				layoutId = R.id.widgetText4_widget_0;
			}
		}
		else if (key.equals("1"))
		{
			if (number == 1)
			{
				layoutId = R.id.widgetText1_widget_1;
			}
			else if (number == 2)
			{
				layoutId = R.id.widgetText2_widget_1;
			}
			else if (number == 3)
			{
				layoutId = R.id.widgetText3_widget_1;
			}
			else if (number == 4)
			{
				layoutId = R.id.widgetText4_widget_1;
			}
		}
		else if (key.equals("2"))
		{
			if (number == 1)
			{
				layoutId = R.id.widgetText1_widget_2;
			}
			else if (number == 2)
			{
				layoutId = R.id.widgetText2_widget_2;
			}
			else if (number == 3)
			{
				layoutId = R.id.widgetText3_widget_2;
			}
			else if (number == 4)
			{
				layoutId = R.id.widgetText4_widget_2;
			}
		}
		else if (key.equals("3"))
		{
			if (number == 1)
			{
				layoutId = R.id.widgetText1_widget_3;
			}
			else if (number == 2)
			{
				layoutId = R.id.widgetText2_widget_3;
			}
			else if (number == 3)
			{
				layoutId = R.id.widgetText3_widget_3;
			}
			else if (number == 4)
			{
				layoutId = R.id.widgetText4_widget_3;
			}
		}
		else if (key.equals("4"))
		{
			if (number == 1)
			{
				layoutId = R.id.widgetText1_widget_4;
			}
			else if (number == 2)
			{
				layoutId = R.id.widgetText2_widget_4;
			}
			else if (number == 3)
			{
				layoutId = R.id.widgetText3_widget_4;
			}
			else if (number == 4)
			{
				layoutId = R.id.widgetText4_widget_4;
			}
		}
		
		return layoutId;
	}
	
	/**
	 * 큰 위젯 투명도 값에 따른 레이아웃의 금액 텍스트 아이디를 반환한다.
	 * @param key
	 * @param number
	 * @return
	 */
	public int getSmallMoneyId(String key, int number)
	{
		int layoutId = 0;
		
		if (key.equals("0"))
		{
			if (number == 1)
			{
				layoutId = R.id.widget2Money1_widget_0;
			}
			else if (number == 2)
			{
				layoutId = R.id.widget2Money2_widget_0;
			}
		}
		else if (key.equals("1"))
		{
			if (number == 1)
			{
				layoutId = R.id.widget2Money1_widget_1;
			}
			else if (number == 2)
			{
				layoutId = R.id.widget2Money2_widget_1;
			}
		}
		else if (key.equals("2"))
		{
			if (number == 1)
			{
				layoutId = R.id.widget2Money1_widget_2;
			}
			else if (number == 2)
			{
				layoutId = R.id.widget2Money2_widget_2;
			}
		}
		else if (key.equals("3"))
		{
			if (number == 1)
			{
				layoutId = R.id.widget2Money1_widget_3;
			}
			else if (number == 2)
			{
				layoutId = R.id.widget2Money2_widget_3;
			}
		}
		else if (key.equals("4"))
		{
			if (number == 1)
			{
				layoutId = R.id.widget2Money1_widget_4;
			}
			else if (number == 2)
			{
				layoutId = R.id.widget2Money2_widget_4;
			}
		}
		
		return layoutId;
	}
	
	/**
	 * 큰 위젯 투명도 값에 따른 레이아웃의 이름 텍스트 아이디를 반환한다.
	 * @param key
	 * @param number
	 * @return
	 */
	public int getSmallTextViewId(String key, int number)
	{
		int layoutId = 0;
		
		if (key.equals("0"))
		{
			if (number == 1)
			{
				layoutId = R.id.widget2Text1_widget_0;
			}
			else if (number == 2)
			{
				layoutId = R.id.widget2Text2_widget_0;
			}
		}
		else if (key.equals("1"))
		{
			if (number == 1)
			{
				layoutId = R.id.widget2Text1_widget_1;
			}
			else if (number == 2)
			{
				layoutId = R.id.widget2Text2_widget_1;
			}
		}
		else if (key.equals("2"))
		{
			if (number == 1)
			{
				layoutId = R.id.widget2Text1_widget_2;
			}
			else if (number == 2)
			{
				layoutId = R.id.widget2Text2_widget_2;
			}
		}
		else if (key.equals("3"))
		{
			if (number == 1)
			{
				layoutId = R.id.widget2Text1_widget_3;
			}
			else if (number == 2)
			{
				layoutId = R.id.widget2Text2_widget_3;
			}
		}
		else if (key.equals("4"))
		{
			if (number == 1)
			{
				layoutId = R.id.widget2Text1_widget_4;
			}
			else if (number == 2)
			{
				layoutId = R.id.widget2Text2_widget_4;
			}
		}
		
		return layoutId;
	}
	
	/**
	 * 큰 위젯 투명도 설정 값에 따른 레이아웃 아이디 값을 반환한다.
	 * @param kay
	 * @return
	 */
	public int getSmallLayout(String key, int type)
	{
		int layoutId = 0;
		
		if (key.equals("0"))
		{
			if (type == 0)
			{
				layoutId = R.layout.accountbook_widget_small_0;
			}
			else
			{
				layoutId = R.layout.accountbook_widget_3x1_small_0;
			}
		}
		else if (key.equals("1"))
		{
			if (type == 0)
			{
				layoutId = R.layout.accountbook_widget_small_1;
			}
			else
			{
				layoutId = R.layout.accountbook_widget_3x1_small_1;
			}
		}
		else if (key.equals("2"))
		{
			if (type == 0)
			{
				layoutId = R.layout.accountbook_widget_small_2;
			}
			else
			{
				layoutId = R.layout.accountbook_widget_3x1_small_2;
			}
		}
		else if (key.equals("3"))
		{
			if (type == 0)
			{
				layoutId = R.layout.accountbook_widget_small_3;
			}
			else
			{
				layoutId = R.layout.accountbook_widget_3x1_small_3;
			}
		}
		else if (key.equals("4"))
		{
			if (type == 0)
			{
				layoutId = R.layout.accountbook_widget_small_4;
			}
			else
			{
				layoutId = R.layout.accountbook_widget_3x1_small_4;
			}
		}
		
		return layoutId;
	}
	
	/**
	 * 타입에 맞는 금액을 반환한다.
	 * flag = 0 위젯사용, flag = 1 메인사용
	 * @param key 타입번호
	 * @param number 자산저장위치
	 * @return
	 */
	public String getMoney(String key, int number, int flag)
	{
		BigDecimal money = BigDecimal.ZERO;
		
		String strMoney = "";
		
		if (key.equals(TOTAL_MONEY))
		{
			money = getToTalMoney();
			
			if (money != null)
			{
				strMoney = BDUtil.getWon(money.toString());
			}
		}
		else if (key.equals(BANK_MONEY))
		{
			money = getBankBookMoney();
			
			if (money != null)
			{
				strMoney = BDUtil.getWon(money.toString());
			}
		}
		else if (key.equals(REAL_MONEY))
		{
			money = getRealMoney();
			
			if (money != null)
			{
				strMoney = BDUtil.getWon(money.toString());
			}
		}
		else if (key.equals(CREDIT_MONEY))
		{
			money = getCreditCardMoney();
			
			if (money != null)
			{
				strMoney = BDUtil.getWon(money.toString());
			}
		}
		else if (key.equals(MONTH_OUT_MONEY))
		{
			money = getMonthOutMoney();
			
			if (money != null)
			{
				strMoney = BDUtil.getWon(money.toString());
			}
		}
		else if (key.equals(MONTH_POST_MONEY))
		{
			money = getMonthPostMoney();
			
			if (money != null)
			{
				strMoney = BDUtil.getWon(money.toString());
			}
		}
		else if (key.equals(MONTH_IN_MONEY))
		{
			money = getMonthInMoney();
			
			if (money != null)
			{
				strMoney = BDUtil.getWon(money.toString());
			}
		}
		else if (key.equals(TODAY_OUT_MONEY))
		{
			money = getTodayOutMoney();
			
			if (money != null)
			{
				strMoney = BDUtil.getWon(money.toString());
			}
		}
		else if (key.equals(TODAY_POST_MONEY))
		{
			money = getTodayPostMoney();
			
			if (money != null)
			{
				strMoney = BDUtil.getWon(money.toString());
			}
		}
		else if (key.equals(TODAY_IN_MONEY))
		{
			money = getTodayInMoney();
			
			if (money != null)
			{
				strMoney = BDUtil.getWon(money.toString());
			}
		}
		else if (key.equals(BANKBOOK))
		{
			money = getBankBookMoney(getMoneyKindName(number, flag));
			
			if (money != null)
			{
				strMoney = BDUtil.getWon(money.toString());
			}
		}
		else if (key.equals(CHECKCARD))
		{
			money = getCheckCardMoney(getMoneyKindName(number, flag));
			
			if (money != null)
			{
				strMoney = BDUtil.getWon(money.toString());
			}
		}
		else if (key.equals(CREDITCARD))
		{
			money = getCreditCardMoney(getMoneyKindName(number, flag));
			
			if (money != null)
			{
				strMoney = BDUtil.getWon(money.toString());
			}
		}
		else if (key.equals(MONTH_MOVE_MONEY))
		{
			money = getMonthMoveMoney();
			
			if (money != null)
			{
				strMoney = BDUtil.getWon(money.toString());
			}
		}
		else if (key.equals(TODAY_MOVE_MONEY))
		{
			money = getTodayMoveMoney();
			
			if (money != null)
			{
				strMoney = BDUtil.getWon(money.toString());
			}
		}
		
		return strMoney;
	}
	
	/**
	 * flag = 0 위젯사용, flag = 1 메인사용
	 * @param key 타입번호
	 * @param number 자산저장위치
	 * @param flag
	 * @return
	 */
	public String getName(String key, int number, int flag)
	{
		String name = "";
		
		if (key.equals(TOTAL_MONEY))
		{
			name = widgetList.get(0);
		}
		else if (key.equals(BANK_MONEY))
		{
			name = widgetList.get(1);
		}
		else if (key.equals(REAL_MONEY))
		{
			name = widgetList.get(2);
		}
		else if (key.equals(CREDIT_MONEY))
		{
			name = widgetList.get(3);
		}
		else if (key.equals(MONTH_OUT_MONEY))
		{
			name = widgetList.get(4);
		}
		else if (key.equals(MONTH_POST_MONEY))
		{
			name = widgetList.get(5);
		}
		else if (key.equals(MONTH_IN_MONEY))
		{
			name = widgetList.get(6);
		}
		else if (key.equals(TODAY_OUT_MONEY))
		{
			name = widgetList.get(7);
		}
		else if (key.equals(TODAY_POST_MONEY))
		{
			name = widgetList.get(8);
		}
		else if (key.equals(TODAY_IN_MONEY))
		{
			name = widgetList.get(9);
		}
		else if (key.equals(BANKBOOK) || key.equals(CHECKCARD) || key.equals(CREDITCARD))
		{
			name = getMoneyKindName(number, flag);
		}
		else if (key.equals(SECRET))
		{
			name = widgetList.get(13);
		}
		else if (key.equals(MONTH_MOVE_MONEY))
		{
			name = widgetList.get(14);
		}
		else if (key.equals(TODAY_MOVE_MONEY))
		{
			name = widgetList.get(15);
		}
		
		return name;
	}
	
	/**
	 * 위젯에 있는 순서를 알아서 자산이름을 반환한다.
	 * @param number
	 * @return
	 */
	private String getMoneyKindName(int number, int flag)
	{
		String name = "";
		
		SharedPreferences pref = context.getSharedPreferences("pref", Activity.MODE_PRIVATE);
		
		if (number == 0)
		{
			if (flag == 0)
			{
				name = pref.getString(BDConstents.BIG_WIDGET_MONEYKIND1, "");
			}
			else
			{
				name = pref.getString(BDConstents.BIG_MAIN_MONEYKIND1, "");
			}
		}
		else if (number == 1)
		{
			if (flag == 0)
			{
				name = pref.getString(BDConstents.BIG_WIDGET_MONEYKIND2, "");
			}
			else
			{
				name = pref.getString(BDConstents.BIG_MAIN_MONEYKIND2, "");
			}
		}
		else if (number == 2)
		{
			if (flag == 0)
			{
				name = pref.getString(BDConstents.BIG_WIDGET_MONEYKIND3, "");
			}
			else
			{
				name = pref.getString(BDConstents.BIG_MAIN_MONEYKIND3, "");
			}
		}
		else if (number == 3)
		{
			if (flag == 0)
			{
				name = pref.getString(BDConstents.BIG_WIDGET_MONEYKIND4, "");
			}
			else
			{
				name = pref.getString(BDConstents.BIG_MAIN_MONEYKIND4, "");
			}
		}
		else if (number == 4)
		{
			if (flag == 0)
			{
				name = pref.getString(BDConstents.SMALL_WIDGET_MONEYKIND1, "");
			}
			else
			{
				name = pref.getString(BDConstents.BIG_MAIN_MONEYKIND5, "");
			}
		}
		else if (number == 5)
		{
			if (flag == 0)
			{
				name = pref.getString(BDConstents.SMALL_WIDGET_MONEYKIND2, "");
			}
			else
			{
				name = pref.getString(BDConstents.BIG_MAIN_MONEYKIND6, "");
			}
		}
		else if (number == 6)
		{
			if (flag == 0)
			{
			}
			else
			{
				name = pref.getString(BDConstents.BIG_MAIN_MONEYKIND7, "");
			}
		}
		else if (number == 7)
		{
			if (flag == 0)
			{
			}
			else
			{
				name = pref.getString(BDConstents.BIG_MAIN_MONEYKIND8, "");
			}
		}
		else if (number == 8)
		{
			if (flag == 0)
			{
			}
			else
			{
				name = pref.getString(BDConstents.BIG_MAIN_MONEYKIND9, "");
			}
		}
		else if (number == 9)
		{
			if (flag == 0)
			{
			}
			else
			{
				name = pref.getString(BDConstents.BIG_MAIN_MONEYKIND10, "");
			}
		}
		
		return name;
	}
	
	/**
	 * 타입에 따른 색을 반환한다.
	 * @param key
	 * @return
	 */
	public int getColor(String key)
	{
		int color = 0x000000;
		
		if (key.equals(TOTAL_MONEY))
		{
			color = Color.parseColor("#000000");
		}
		else if (key.equals(BANK_MONEY))
		{
			color = Color.parseColor("#000000");
		}
		else if (key.equals(REAL_MONEY))
		{
			color = Color.parseColor("#000000");
		}
		else if (key.equals(CREDIT_MONEY))
		{
			color = Color.parseColor("#40a940");
		}
		else if (key.equals(MONTH_OUT_MONEY))
		{
			color = Color.parseColor("#ff5050");
		}
		else if (key.equals(MONTH_POST_MONEY))
		{
			color = Color.parseColor("#40a940");
		}
		else if (key.equals(MONTH_IN_MONEY))
		{
			color = Color.parseColor("#1e96ff");
		}
		else if (key.equals(TODAY_OUT_MONEY))
		{
			color = Color.parseColor("#ff5050");
		}
		else if (key.equals(TODAY_POST_MONEY))
		{
			color = Color.parseColor("#40a940");
		}
		else if (key.equals(TODAY_IN_MONEY))
		{
			color = Color.parseColor("#1e96ff");
		}
		else if (key.equals(BANKBOOK) || key.equals(CHECKCARD))
		{
			color = Color.parseColor("#000000");
		}
		else if (key.equals(CREDITCARD))
		{
			color = Color.parseColor("#40a940");
		}
		else if (key.equals(SECRET))
		{
			color = Color.parseColor("#ef8b47");
		}
		else if (key.equals(MONTH_MOVE_MONEY))
		{
			color = Color.parseColor("#ef8b47");
		}
		else if (key.equals(TODAY_MOVE_MONEY))
		{
			color = Color.parseColor("#ef8b47");
		}
		
		return color;
	}
	
	/**
	 * 시작일을 셋팅한다.
	 */
	private void setStartDate()
	{
		SharedPreferences pref = context.getSharedPreferences("pref", Activity.MODE_PRIVATE);
		startDate = Integer.parseInt(pref.getString(BDConstents.START_DATE, "1"));
		
		// 시작일이 1일이면 원래대로
    	if (startDate == 1) 
    	{
    		firstDate = BDUtil.getDateDB(toYear, toMonth+1, 1);
    		lastDate = BDUtil.getDateDB(toYear, toMonth+1, 31); 
    	}
    	else
    	{
    		if (toMonth+1 == 1)
    		{
    			// 1월이면 
    			if (toDay < startDate)
    			{
    				// 저번달에 낑겨있으면
    				// 1월이면 작년 12월 시작일부터 올해 이번달 시작일전날까지
        			// 이달의 지출
    				firstDate = BDUtil.getDateDB(toYear-1, 12, startDate);
    				lastDate = BDUtil.getDateDB(toYear, 1, startDate-1);
    			}
    			else
    			{
    				// 새달이 시작됐으면
    				// 1월이면 1월 시작일부터 2월 시작일전날까지
        			// 이달의 지출
    				firstDate = BDUtil.getDateDB(toYear, 1, startDate);
    				lastDate = BDUtil.getDateDB(toYear, 2, startDate-1);
    			}    			
    		}
    		else if (toMonth+1 == 12)
    		{
    			// 12월이면 
    			if (toDay < startDate)
    			{
    				// 저번달에 낑겨있으면
    				// 올해 11월 시작일부터 올해 12월 시작일전날까지
        			// 이달의 지출
    				firstDate = BDUtil.getDateDB(toYear, 11, startDate);
    				lastDate = BDUtil.getDateDB(toYear, 12, startDate-1);
    			}
    			else
    			{
    				// 새달이 시작됐으면
    				// 작년 12월 시작일부터 1월 시작일전날까지
        			// 이달의 지출
    				firstDate = BDUtil.getDateDB(toYear, 12, startDate);
    				lastDate = BDUtil.getDateDB(toYear+1, 1, startDate-1);
    			}
    		}
    		else
    		{
    			// 1월이 아니면
    			if (toDay < startDate)
    			{
    				// 전달에 낑겨있으면
    				// 이달의 지출
    				firstDate = BDUtil.getDateDB(toYear, toMonth, startDate);
    				lastDate = BDUtil.getDateDB(toYear, toMonth+1, startDate-1);
    			}
    			else
    			{
    				// 새달이 시작됐으면
    				// 이달의 지출
    				firstDate = BDUtil.getDateDB(toYear, toMonth+1, startDate);
    				lastDate = BDUtil.getDateDB(toYear, toMonth+2, startDate-1);
    			}
    		}
    	}
	}
	
	/**
	 * 날짜를 셋팅한다.
	 * @param firstDate
	 * @param lastDate
	 */
	public void setDate(int toYear, int toMonth, int toDay)
	{
		this.toYear = toYear;
		this.toMonth = toMonth;
		this.toDay = toDay;
		
		setStartDate();
	}
	
	/**
	 * 한달을 차감한다.
	 */
	public void setLeftMonth()
	{
		Calendar cal = Calendar.getInstance();
		cal.set(toYear, toMonth, toDay);
		cal.add(Calendar.MONTH, -1);
		
		toYear = cal.get(Calendar.YEAR);
		toMonth = cal.get(Calendar.MONTH);
		toDay = cal.get(Calendar.DAY_OF_MONTH);
		
		setStartDate();
	}
	
	/**
	 * 한달을 더한다.
	 */
	public void setRightMonth()
	{
		Calendar cal = Calendar.getInstance();
		cal.set(toYear, toMonth, toDay);
		cal.add(Calendar.MONTH, 1);
		
		toYear = cal.get(Calendar.YEAR);
		toMonth = cal.get(Calendar.MONTH);
		toDay = cal.get(Calendar.DAY_OF_MONTH);
		
		setStartDate();
	}
	
	/**
	 * 하루를 차감한다.
	 */
	public void setLeftToday()
	{
		Calendar cal = Calendar.getInstance();
		cal.set(toYear, toMonth, toDay);
		cal.add(Calendar.DAY_OF_MONTH, -1);
		
		toYear = cal.get(Calendar.YEAR);
		toMonth = cal.get(Calendar.MONTH);
		toDay = cal.get(Calendar.DAY_OF_MONTH);
		
		setStartDate();
	}
	
	/**
	 * 하루를 더한다.
	 */
	public void setRightToday()
	{
		Calendar cal = Calendar.getInstance();
		cal.set(toYear, toMonth, toDay);
		cal.add(Calendar.DAY_OF_MONTH, 1);
		
		toYear = cal.get(Calendar.YEAR);
		toMonth = cal.get(Calendar.MONTH);
		toDay = cal.get(Calendar.DAY_OF_MONTH);
		
		setStartDate();
	}
	
	/**
	 * 년을 반환한다.
	 * @return
	 */
	public int getYear()
	{
		return this.toYear;
	}
	
	/**
	 * 월을 반환한다.
	 * @return
	 */
	public int getMonth()
	{
		return this.toMonth;
	}
	
	/**
	 * 일을 반환한다.
	 * @return
	 */
	public int getDay()
	{
		return this.toDay;
	}

	/**
	 * 총 통장 금액을 가져온다.
	 * @return
	 */
	public BigDecimal getBankBookMoney()
	{
		sql.selectMapTile(context, "SELECT id, money FROM bankbook;", new ParseSelectData()
		{
			@Override
			public void resultData(Cursor c)
			{
				int count = c.getCount();
				
				int idIndex = c.getColumnIndex("id");
				int columIndex = c.getColumnIndex("money");
				c.moveToFirst();
				money = BigDecimal.ZERO;
				for (int i = 0; i < count; i++)
				{
//					money = money + Integer.parseInt(c.getString(columIndex));
					
					if (getAddBankBookFlag(c.getInt(idIndex)))
					{
						money = money.add(new BigDecimal( (c.getString(columIndex)).replaceAll(",", "") ));
					}
					
					c.moveToNext();
				}
			}
		});
		
		return money;
	}
	
	/**
	 * 하나의 통장 금액을 가져온다.
	 * @return
	 */
	public BigDecimal getBankBookMoney(String name)
	{
		sql.selectMapTile(context, "SELECT id, money FROM bankbook WHERE name = '" +
				BDUtil.getApostrophe(name) + "';", new ParseSelectData()
		{
			@Override
			public void resultData(Cursor c)
			{
				int count = c.getCount();
				
				int idIndex = c.getColumnIndex("id");
				int columIndex = c.getColumnIndex("money");
				c.moveToFirst();
				money = BigDecimal.ZERO;
				for (int i = 0; i < count; i++)
				{
//					money = money + Integer.parseInt(c.getString(columIndex));
					
					money = money.add(new BigDecimal((c.getString(columIndex)).replaceAll(",", "")));
					
					c.moveToNext();
				}
			}
		});
		
		return money;
	}
	
	/**
	 * 하나의 통장 금액을 가져온다.
	 * @return
	 */
	public BigDecimal getBankBookMoneyId(String id)
	{
		sql.selectMapTile(context, "SELECT id, money FROM bankbook WHERE id = " +
				id + ";", new ParseSelectData()
		{
			@Override
			public void resultData(Cursor c)
			{
				int count = c.getCount();
				
				int idIndex = c.getColumnIndex("id");
				int columIndex = c.getColumnIndex("money");
				c.moveToFirst();
				money = BigDecimal.ZERO;
				for (int i = 0; i < count; i++)
				{
//					money = money + Integer.parseInt(c.getString(columIndex));
					
					money = money.add(new BigDecimal((c.getString(columIndex)).replaceAll(",", "")));
					
					c.moveToNext();
				}
			}
		});
		
		return money;
	}
	
	/**
	 * 하나의 체크카드 금액을 가져온다.
	 * @param name
	 * @return
	 */
	public BigDecimal getCheckCardMoney(String name)
	{
		sql.selectMapTile(context, "SELECT id, banklink FROM check_card WHERE name = '" +
				BDUtil.getApostrophe(name) + "';", new ParseSelectData()
		{
			@Override
			public void resultData(Cursor c)
			{
				int count = c.getCount();
				
				int idIndex = c.getColumnIndex("id");
				int banklinkIndex = c.getColumnIndex("banklink");
				c.moveToFirst();
				money = BigDecimal.ZERO;
				for (int i = 0; i < count; i++)
				{
//					money = money + Integer.parseInt(c.getString(columIndex));
					
					money = money.add(getBankBookMoneyId(c.getInt(banklinkIndex) + ""));
					
					c.moveToNext();
				}
			}
		});
		
		return money;
	}
	
	/**
	 * 총 현금을 가져온다.
	 * @return
	 */
	public BigDecimal getRealMoney()
	{
		sql.selectMapTile(context, "SELECT money FROM real_money;", new ParseSelectData()
		{
			@Override
			public void resultData(Cursor c)
			{
				int count = c.getCount();
				
				int columIndex = c.getColumnIndex("money");
				c.moveToFirst();
				money = BigDecimal.ZERO;
				for (int i = 0; i < count; i++)
				{
//					money = money + Integer.parseInt(c.getString(columIndex));
					money = money.add(new BigDecimal((c.getString(columIndex)).replaceAll(",", "")));
					c.moveToNext();
				}
			}
		});
		
		return money;
	}
	
	/**
	 * 총 카드대금을 가져온다.
	 * @return
	 */
	public BigDecimal getCreditCardMoney()
	{
		sql.selectMapTile(context, "SELECT outmoney FROM credit_card;", new ParseSelectData()
		{
			@Override
			public void resultData(Cursor c)
			{
				int count = c.getCount();
				
				int columIndex = c.getColumnIndex("outmoney");
				c.moveToFirst();
				money = BigDecimal.ZERO;
				for (int i = 0; i < count; i++)
				{
//					money = money + Integer.parseInt(c.getString(columIndex));
					money = money.add(new BigDecimal((c.getString(columIndex)).replaceAll(",", "")));
					c.moveToNext();
				}
			}
		});
		
		return money;
	}
	
	/**
	 * 하나의 카드대금을 가져온다.
	 * @return
	 */
	public BigDecimal getCreditCardMoney(String name)
	{
		sql.selectMapTile(context, "SELECT outmoney FROM credit_card WHERE name = '" +
				BDUtil.getApostrophe(name) + "';", new ParseSelectData()
		{
			@Override
			public void resultData(Cursor c)
			{
				int count = c.getCount();
				
				int columIndex = c.getColumnIndex("outmoney");
				c.moveToFirst();
				money = BigDecimal.ZERO;
				for (int i = 0; i < count; i++)
				{
//					money = money + Integer.parseInt(c.getString(columIndex));
					money = money.add(new BigDecimal((c.getString(columIndex)).replaceAll(",", "")));
					c.moveToNext();
				}
			}
		});
		
		return money;
	}
	
	/**
	 * 총 자산을 가져온다.
	 * @return
	 */
	public BigDecimal getToTalMoney()
	{
		BigDecimal money = BigDecimal.ZERO;
		
		money = money.add(getBankBookMoney());
		
		money = money.add(getRealMoney());
		
		return money;
	}
	
	/**
	 * 이달의 지출을 가져온다.
	 * @return
	 */
	public BigDecimal getMonthOutMoney()
	{
		SharedPreferences pref = context.getSharedPreferences("pref", Activity.MODE_PRIVATE);
		
		int fanOkNoFlag = pref.getInt(BDConstents.FAN_SETTING_OK_NO, 0);
		
		String fanSql = "";
		
		// 부채 포함여부에 따른 쿼리
    	if (fanOkNoFlag != 0)
		{
			fanSql = BDConstents.FAN_TYPE + ", ";
		}
		
		String sqlQuery = "SELECT outmoney FROM in_out WHERE date >= " + firstDate + " and date <= " +
	     		   lastDate + " and type not in (" 
	     		   + BDConstents.INOUT_TYPE + ", "
    			   + BDConstents.OUTIN_TYPE + ", " + fanSql
    			   + BDConstents.MYBANK_INOUT_TYPE + ") "
    			   + "UNION ALL "
	 			   + "SELECT outmoney FROM in_out WHERE date >= " + firstDate
	 			   + " and date <= " + lastDate
	 			   + " and category in ('" 
	 			   + BDConstents.CATEGORY_SUSUMONEY + "')";;
	    	
	 	sql.selectMapTile(context, sqlQuery, new ParseSelectData()
		{
			@Override
			public void resultData(Cursor c)
			{
				int count = c.getCount();
				
				int columIndex = c.getColumnIndex("outmoney");
				c.moveToFirst();
//					int money = 0;
				money = BigDecimal.ZERO;
				for (int i = 0; i < count; i++)
				{
//						money = money + Integer.parseInt(c.getString(columIndex));
					money = money.add(new BigDecimal((c.getString(columIndex)).replaceAll(",", "")));
					c.moveToNext();
				}
			}
		});
		
		return money;
	}
	
	/**
	 * 이달의 카드대금을 가져온다.
	 * @return
	 */
	public BigDecimal getMonthPostMoney()
	{
		SharedPreferences pref = context.getSharedPreferences("pref", Activity.MODE_PRIVATE);
		/** 시작일로 카드대금 가져올지 기간으로 가져올지 플래그 */
		int postCheck = pref.getInt(BDConstents.MONTH_POSTMONEY_SETTING, 0);
		
		String sqlQuery = "";
		
		int fanOkNoFlag = pref.getInt(BDConstents.FAN_SETTING_OK_NO, 0);
		
		String fanSql = "";
		
		// 부채 포함여부에 따른 쿼리
    	if (fanOkNoFlag != 0)
		{
			fanSql = " and type not in ( " + BDConstents.FAN_TYPE + " )";
		}
		
		if (postCheck == 0)
		{
			sqlQuery = "SELECT postmoney FROM in_out WHERE date >= " 
					+ firstDate + " and date <= " 
					+ lastDate + fanSql + ";";
			
	    	sql.selectMapTile(context, sqlQuery, new ParseSelectData()
			{
				@Override
				public void resultData(Cursor c)
				{
					int count = c.getCount();
					
					int columIndex = c.getColumnIndex("postmoney");
					c.moveToFirst();
//					int money = 0;
					money = BigDecimal.ZERO;
					for (int i = 0; i < count; i++)
					{
//						money = money + Integer.parseInt(c.getString(columIndex));
						money = money.add(new BigDecimal((c.getString(columIndex)).replaceAll(",", "")));
						c.moveToNext();
					}
				}
			});
		}
		else
		{
			setPostMoneyTerm();
		}
		
		return money;
	}
	
	/**
	 * 카드 사용 일자로 카드 사용 금액을 가져온다.
	 */
	private void setPostMoneyTerm()
	{
		SharedPreferences pref = context.getSharedPreferences("pref", Activity.MODE_PRIVATE);
		
		int fanOkNoFlag = pref.getInt(BDConstents.FAN_SETTING_OK_NO, 0);
		
		String fanSql = "";
		
		// 부채 포함여부에 따른 쿼리
    	if (fanOkNoFlag != 0)
		{
    		fanSql = " and type not in ( " + BDConstents.FAN_TYPE + " )";
		}
		
		creditResultList = new ArrayList<CreditCardResultDateData>();
		
		sql.selectMapTile(context, "SELECT * FROM credit_card_result_date;", new ParseSelectData()
		{
			@Override
			public void resultData(Cursor c)
			{
				int idIdx = c.getColumnIndex("id");
				int nameIdx = c.getColumnIndex("name");
				int autoynIdx = c.getColumnIndex("autoyn");
				int startdateIdx = c.getColumnIndex("startdate");
				int enddateIdx = c.getColumnIndex("enddate");
				int resultdateIdx = c.getColumnIndex("resultdate");
				int resultflagIdx = c.getColumnIndex("resultflag");
				int linkidIdx = c.getColumnIndex("linkid");
				
				c.moveToFirst();
				
				for (int i = 0; i < c.getCount(); i++)
				{
					CreditCardResultDateData creditResultData = new CreditCardResultDateData();
					
					creditResultData.setId(c.getInt(idIdx) + "");
					creditResultData.setName(c.getString(nameIdx));
					creditResultData.setAutoyn(c.getInt(autoynIdx) + "");
					creditResultData.setStartdate(c.getInt(startdateIdx) + "");
					creditResultData.setEnddate(c.getInt(enddateIdx) + "");
					creditResultData.setResultdate(c.getInt(resultdateIdx) + "");
					creditResultData.setResultflag(c.getInt(resultflagIdx) + "");
					creditResultData.setLinkid(c.getInt(linkidIdx) + "");
					
					creditResultList.add(creditResultData);
					
					c.moveToNext();
				}
			}
		});
		
		money = BigDecimal.ZERO;
		
		for (int i = 0; i < creditResultList.size(); i++)
		{
			CreditCardResultDateData data = creditResultList.get(i);
			
			String moneykind = data.getName();
			
			int startDay = Integer.parseInt(data.getStartdate());
			int endDay = Integer.parseInt(data.getEnddate());
			
			String firstDate = "";
			String lastDate = "";
			
			if (toDay >= startDay)
			{
				if (startDay == 1)
				{
					firstDate = BDUtil.getDateDB(toYear, toMonth+1, startDay);
					lastDate = BDUtil.getDateDB(toYear, toMonth+1, endDay);
				}
				else if (toMonth == 11)
				{
					firstDate = BDUtil.getDateDB(toYear, toMonth+1, startDay);
					lastDate = BDUtil.getDateDB(toYear+1, 1, endDay);
				}
				else
				{
					firstDate = BDUtil.getDateDB(toYear, toMonth+1, startDay);
					lastDate = BDUtil.getDateDB(toYear, toMonth+2, endDay);
				}
			}
			else
			{
				if (startDay == 1)
				{
					firstDate = BDUtil.getDateDB(toYear, toMonth, endDay);
					lastDate = BDUtil.getDateDB(toYear, toMonth, endDay);
				}
				else if (toMonth == 0)
				{
					firstDate = BDUtil.getDateDB(toYear-1, 12, startDay);
					lastDate = BDUtil.getDateDB(toYear, toMonth+1, endDay);
				}
				else
				{
					firstDate = BDUtil.getDateDB(toYear, toMonth, startDay);
					lastDate = BDUtil.getDateDB(toYear, toMonth+1, endDay);
				}
			}
			
			if (firstDate.equals(""))
			{
				continue;
			}
			
			String sqlQuery = "SELECT postmoney FROM in_out WHERE date >= " 
					+ firstDate + " and date <= " 
					+ lastDate + " and moneykind = '" 
					+ BDUtil.getApostrophe(moneykind) + "'" + fanSql + ";";
			
			sql.selectMapTile(context, sqlQuery, new ParseSelectData()
			{
				@Override
				public void resultData(Cursor c)
				{
					int count = c.getCount();
					
					int columIndex = c.getColumnIndex("postmoney");
					c.moveToFirst();
//					int money = 0;
					
					for (int i = 0; i < count; i++)
					{
//						money = money + Integer.parseInt(c.getString(columIndex));
						money = money.add(new BigDecimal((c.getString(columIndex)).replaceAll(",", "")));
						c.moveToNext();
					}
				}
			});
		}
	}
	
	/**
	 * 이달의 총 수입을 가져온다.
	 * @return
	 */
	public BigDecimal getMonthInMoney()
	{
		SharedPreferences pref = context.getSharedPreferences("pref", Activity.MODE_PRIVATE);
		
		int fanOkNoFlag = pref.getInt(BDConstents.FAN_SETTING_OK_NO, 0);
		
		String fanSql = "";
		
		// 부채 포함여부에 따른 쿼리
    	if (fanOkNoFlag != 0)
		{
			fanSql = BDConstents.FAN_TYPE + ", ";
		}
		
		String sqlQuery = "SELECT inmoney FROM in_out WHERE date >= " + firstDate + " and date <= " +
				lastDate + " and type not in (" 
	   			+ BDConstents.INOUT_TYPE + ", " 
	   			+ BDConstents.OUTIN_TYPE + ", " + fanSql 
	   			+ BDConstents.MYBANK_INOUT_TYPE + ")"
	   			+ "UNION ALL "
	   			+ "SELECT inmoney FROM in_out WHERE date >= " + firstDate
	   			+ " and date <= " + lastDate
	   			+ " and category in ('" 
	   			+ BDConstents.CATEGORY_SUSUMONEY + "')";
    	
		sql.selectMapTile(context, sqlQuery, new ParseSelectData()
		{
			@Override
			public void resultData(Cursor c)
			{
				int count = c.getCount();
				
				int columIndex = c.getColumnIndex("inmoney");
				c.moveToFirst();
//				int money = 0;
				money = BigDecimal.ZERO;
				for (int i = 0; i < count; i++)
				{
//					money = money + Integer.parseInt(c.getString(columIndex));
					money = money.add(new BigDecimal((c.getString(columIndex)).replaceAll(",", "")));
					c.moveToNext();
				}
			}
		});
		
		return money;
	}
	
	/**
	 * 오늘의 지출을 가져온다.
	 * @return
	 */
	public BigDecimal getTodayOutMoney()
	{
		SharedPreferences pref = context.getSharedPreferences("pref", Activity.MODE_PRIVATE);
		
		int fanOkNoFlag = pref.getInt(BDConstents.FAN_SETTING_OK_NO, 0);
		
		String fanSql = "";
		
		// 부채 포함여부에 따른 쿼리
    	if (fanOkNoFlag != 0)
		{
			fanSql = BDConstents.FAN_TYPE + ", ";
		}
		
		String sqlQuery = "SELECT outmoney FROM in_out WHERE outmoney not in ('0') and date = "
    			+ BDUtil.getDateDB(toYear, toMonth+1, toDay) + " and type not in (" 
    			+ BDConstents.INOUT_TYPE + ", " 
    			+ BDConstents.OUTIN_TYPE + ", " + fanSql
    			+ BDConstents.MYBANK_INOUT_TYPE + ")"
    			+ "UNION ALL "
			    + "SELECT outmoney FROM in_out WHERE date = " + BDUtil.getDateDB(toYear, toMonth+1, toDay) 
			    + " and category in ('" 
			    + BDConstents.CATEGORY_SUSUMONEY + "')";
    	
    	sql.selectMapTile(context, sqlQuery, new ParseSelectData()
		{
			@Override
			public void resultData(Cursor c)
			{
				int count = c.getCount();
				
				int columIndex = c.getColumnIndex("outmoney");
				c.moveToFirst();
//				int money = 0;
				money = BigDecimal.ZERO;
				for (int i = 0; i < count; i++)
				{
//					money = money + Integer.parseInt(c.getString(columIndex));
					money = money.add(new BigDecimal((c.getString(columIndex)).replaceAll(",", "")));
					c.moveToNext();
				}
			}
		});
		
		return money;
	}
	
	private ArrayList<StatsCategoryData> moveMonthList;
	/**
	 * 이달의 이동을 가져온다.
	 * @return
	 */
	public BigDecimal getMonthMoveMoney()
	{
		moveMonthList = new ArrayList<StatsCategoryData>();
		
		BigDecimal sumMoney = BigDecimal.ZERO;
		
		String inOutQuery = "SELECT category, SUM(outmoney) omoney, SUM(postmoney) pmoney " +
				"FROM in_out WHERE date >= " +
				firstDate + " and date <= " +
				lastDate + " and type = " + 
				BDConstents.INOUT_TYPE + 
				" GROUP BY category;";
		
		sql.selectMapTile(context, inOutQuery, new ParseSelectData()
		{
			@Override
			public void resultData(Cursor c)
			{
				int categoryIdx = c.getColumnIndex("category");
				int omoneyIdx = c.getColumnIndex("omoney");
				int pmoneyIdx = c.getColumnIndex("pmoney");
				
				BigDecimal omoney = BigDecimal.ZERO;
				String name = "";
									
				c.moveToFirst();
				
				for (int i = 0; i < c.getCount(); i++)
				{
					StatsCategoryData data = new StatsCategoryData();
					name = c.getString(categoryIdx);
					omoney = new BigDecimal(c.getString(omoneyIdx).replaceAll(",", ""));
					omoney = omoney.add(new BigDecimal(c.getString(pmoneyIdx).replaceAll(",", "")));
					data.setCategory(name);
					
					data.setMoney(omoney.toString());
					moveMonthList.add(data);
					
					c.moveToNext();
				}
			}
		});
		
		inOutQuery = "SELECT category, SUM(inmoney) imoney " +
				"FROM in_out WHERE date >= " +
				firstDate + " and date <= " +
				lastDate + " and type = " + 
				BDConstents.OUTIN_TYPE + 
				" GROUP BY category;";
		
		sql.selectMapTile(context, inOutQuery, new ParseSelectData()
		{
			@Override
			public void resultData(Cursor c)
			{
				int categoryIdx = c.getColumnIndex("category");
				int imoneyIdx = c.getColumnIndex("imoney");
				
				BigDecimal imoney = BigDecimal.ZERO;
				String name = "";
									
				
				c.moveToFirst();
				
				for (int i = 0; i < c.getCount(); i++)
				{
					StatsCategoryData data = new StatsCategoryData();
					name = c.getString(categoryIdx);
					imoney = new BigDecimal(c.getString(imoneyIdx).replaceAll(",", ""));
					data.setCategory(name);
					
					data.setMoney(imoney.toString());
					
					boolean flag = false;
					
					for (int j = 0; j < moveMonthList.size(); j++)
					{
						if (data.getCategory().equals(moveMonthList.get(j).getCategory()))
						{
							BigDecimal tempMoney = new BigDecimal(moveMonthList.get(j).getMoney().replaceAll(",", ""));
							
							moveMonthList.get(j).setMoney(tempMoney.add(new BigDecimal(data.getMoney())).toString().replaceAll(",", ""));
							
							flag = true;
						}
					}
					
					if (flag == false)
					{
						moveMonthList.add(data);
					}
					
					c.moveToNext();
				}
			}
		});
		
		inOutQuery = "SELECT type, category, SUM(outmoney) omoney, " +
				"SUM(inmoney) imoney, SUM(postmoney) pmoney " +
				"FROM in_out WHERE date >= " +
				firstDate + " and date <= " +
				lastDate + " and type = " + 
				BDConstents.MYBANK_INOUT_TYPE + 
				" GROUP BY category;";
		
		sql.selectMapTile(context, inOutQuery, new ParseSelectData()
		{
			@Override
			public void resultData(Cursor c)
			{
				int categoryIdx = c.getColumnIndex("category");
				int omoneyIdx = c.getColumnIndex("omoney");
				int pmoneyIdx = c.getColumnIndex("pmoney");
				int imoneyIdx = c.getColumnIndex("imoney");
				
				BigDecimal omoney = BigDecimal.ZERO;
				BigDecimal pmoney = BigDecimal.ZERO;
				BigDecimal imoney = BigDecimal.ZERO;
				String name = "";
									
				c.moveToFirst();
				
				for (int i = 0; i < c.getCount(); i++)
				{
					StatsCategoryData data = new StatsCategoryData();
					name = c.getString(categoryIdx);
					omoney = new BigDecimal(c.getString(omoneyIdx).replaceAll(",", ""));
					pmoney = new BigDecimal(c.getString(pmoneyIdx).replaceAll(",", ""));
					imoney = new BigDecimal(c.getString(imoneyIdx).replaceAll(",", ""));
					data.setCategory(name);
					
					omoney = omoney.add(pmoney);
					
					data.setMoney(omoney.toString());
					
					boolean flag = false;
					
					for (int j = 0; j < moveMonthList.size(); j++)
					{
						if (data.getCategory().equals(moveMonthList.get(j).getCategory()))
						{
							BigDecimal tempMoney = new BigDecimal(moveMonthList.get(j).getMoney().replaceAll(",", ""));
							
							moveMonthList.get(j).setMoney(tempMoney.add(new BigDecimal(data.getMoney())).toString().replaceAll(",", ""));
							
							flag = true;
						}
					}
					
					if (flag == false)
					{
						moveMonthList.add(data);
					}
					
					c.moveToNext();
				}
			}
		});
		
		for (int i = 0; i < moveMonthList.size(); i++)
		{
			sumMoney = sumMoney.add(new BigDecimal(moveMonthList.get(i).getMoney().replaceAll(",", "")));
		}
		
		return sumMoney;
	}
	
	private ArrayList<StatsCategoryData> moveTodayList;
	/**
	 * 오늘의 이동을 가져온다.
	 * @return
	 */
	public BigDecimal getTodayMoveMoney()
	{
		moveTodayList = new ArrayList<StatsCategoryData>();
		
		BigDecimal sumMoney = BigDecimal.ZERO;
		
		String inOutQuery = "SELECT category, SUM(outmoney) omoney, SUM(postmoney) pmoney " +
				"FROM in_out WHERE date = " + BDUtil.getDateDB(toYear, toMonth+1, toDay) +
				" and type = " + 
				BDConstents.INOUT_TYPE + 
				" GROUP BY category;";
		
		sql.selectMapTile(context, inOutQuery, new ParseSelectData()
		{
			@Override
			public void resultData(Cursor c)
			{
				int categoryIdx = c.getColumnIndex("category");
				int omoneyIdx = c.getColumnIndex("omoney");
				int pmoneyIdx = c.getColumnIndex("pmoney");
				
				BigDecimal omoney = BigDecimal.ZERO;
				String name = "";
									
				c.moveToFirst();
				
				for (int i = 0; i < c.getCount(); i++)
				{
					StatsCategoryData data = new StatsCategoryData();
					name = c.getString(categoryIdx);
					omoney = new BigDecimal(c.getString(omoneyIdx).replaceAll(",", ""));
					omoney = omoney.add(new BigDecimal(c.getString(pmoneyIdx).replaceAll(",", "")));
					data.setCategory(name);
					
					data.setMoney(omoney.toString());
					moveTodayList.add(data);
					
					c.moveToNext();
				}
			}
		});
		
		inOutQuery = "SELECT category, SUM(inmoney) imoney " +
				"FROM in_out WHERE date = " + BDUtil.getDateDB(toYear, toMonth+1, toDay) +
				" and type = " + 
				BDConstents.OUTIN_TYPE + 
				" GROUP BY category;";
		
		sql.selectMapTile(context, inOutQuery, new ParseSelectData()
		{
			@Override
			public void resultData(Cursor c)
			{
				int categoryIdx = c.getColumnIndex("category");
				int imoneyIdx = c.getColumnIndex("imoney");
				
				BigDecimal imoney = BigDecimal.ZERO;
				String name = "";
									
				
				c.moveToFirst();
				
				for (int i = 0; i < c.getCount(); i++)
				{
					StatsCategoryData data = new StatsCategoryData();
					name = c.getString(categoryIdx);
					imoney = new BigDecimal(c.getString(imoneyIdx).replaceAll(",", ""));
					data.setCategory(name);
					
					data.setMoney(imoney.toString());
					
					boolean flag = false;
					
					for (int j = 0; j < moveTodayList.size(); j++)
					{
						if (data.getCategory().equals(moveTodayList.get(j).getCategory()))
						{
							BigDecimal tempMoney = new BigDecimal(moveTodayList.get(j).getMoney().replaceAll(",", ""));
							
							moveTodayList.get(j).setMoney(tempMoney.add(new BigDecimal(data.getMoney())).toString().replaceAll(",", ""));
							
							flag = true;
						}
					}
					
					if (flag == false)
					{
						moveTodayList.add(data);
					}
					
					c.moveToNext();
				}
			}
		});
		
		inOutQuery = "SELECT type, category, SUM(outmoney) omoney, " +
				"SUM(inmoney) imoney, SUM(postmoney) pmoney " +
				"FROM in_out WHERE date = " + BDUtil.getDateDB(toYear, toMonth+1, toDay) +
				" and type = " + 
				BDConstents.MYBANK_INOUT_TYPE + 
				" GROUP BY category;";
		
		sql.selectMapTile(context, inOutQuery, new ParseSelectData()
		{
			@Override
			public void resultData(Cursor c)
			{
				int categoryIdx = c.getColumnIndex("category");
				int omoneyIdx = c.getColumnIndex("omoney");
				int pmoneyIdx = c.getColumnIndex("pmoney");
				int imoneyIdx = c.getColumnIndex("imoney");
				
				BigDecimal omoney = BigDecimal.ZERO;
				BigDecimal pmoney = BigDecimal.ZERO;
				BigDecimal imoney = BigDecimal.ZERO;
				String name = "";
									
				c.moveToFirst();
				
				for (int i = 0; i < c.getCount(); i++)
				{
					StatsCategoryData data = new StatsCategoryData();
					name = c.getString(categoryIdx);
					omoney = new BigDecimal(c.getString(omoneyIdx).replaceAll(",", ""));
					pmoney = new BigDecimal(c.getString(pmoneyIdx).replaceAll(",", ""));
					imoney = new BigDecimal(c.getString(imoneyIdx).replaceAll(",", ""));
					data.setCategory(name);
					
					omoney = omoney.add(pmoney);
					
					data.setMoney(omoney.toString());
					
					boolean flag = false;
					
					for (int j = 0; j < moveTodayList.size(); j++)
					{
						if (data.getCategory().equals(moveTodayList.get(j).getCategory()))
						{
							BigDecimal tempMoney = new BigDecimal(moveTodayList.get(j).getMoney().replaceAll(",", ""));
							
							moveTodayList.get(j).setMoney(tempMoney.add(new BigDecimal(data.getMoney())).toString().replaceAll(",", ""));
							
							flag = true;
						}
					}
					
					if (flag == false)
					{
						moveTodayList.add(data);
					}
					
					c.moveToNext();
				}
			}
		});
		
		for (int i = 0; i < moveTodayList.size(); i++)
		{
			sumMoney = sumMoney.add(new BigDecimal(moveTodayList.get(i).getMoney().replaceAll(",", "")));
		}
		
		return sumMoney;
	}
	
	/**
	 * 오늘의 카드대금을 가져온다.
	 * @return
	 */
	public BigDecimal getTodayPostMoney()
	{
		SharedPreferences pref = context.getSharedPreferences("pref", Activity.MODE_PRIVATE);
		
		int fanOkNoFlag = pref.getInt(BDConstents.FAN_SETTING_OK_NO, 0);
		
		String fanSql = "";
		
		// 부채 포함여부에 따른 쿼리
    	if (fanOkNoFlag != 0)
		{
    		fanSql = " and type not in ( " + BDConstents.FAN_TYPE + " )";
		}
		
		String sqlQuery = "SELECT postmoney FROM in_out WHERE postmoney not in ('0') and date = "
    			+ BDUtil.getDateDB(toYear, toMonth+1, toDay) + fanSql + ";";
    	
    	sql.selectMapTile(context, sqlQuery, new ParseSelectData()
		{
			@Override
			public void resultData(Cursor c)
			{
				int count = c.getCount();
				
				int columIndex = c.getColumnIndex("postmoney");
				c.moveToFirst();
//				int money = 0;
				money = BigDecimal.ZERO;
				for (int i = 0; i < count; i++)
				{
//					money = money + Integer.parseInt(c.getString(columIndex));
					money = money.add(new BigDecimal(c.getString(columIndex).replaceAll(",", "")));
					c.moveToNext();
				}
			}
		});
		
		return money;
	}
	
	/**
	 * 오늘의 수입을 가져온다.
	 * @return
	 */
	public BigDecimal getTodayInMoney()
	{
		SharedPreferences pref = context.getSharedPreferences("pref", Activity.MODE_PRIVATE);
		
		int fanOkNoFlag = pref.getInt(BDConstents.FAN_SETTING_OK_NO, 0);
		
		String fanSql = "";
		
		// 부채 포함여부에 따른 쿼리
    	if (fanOkNoFlag != 0)
		{
			fanSql = BDConstents.FAN_TYPE + ", ";
		}
		
		String sqlQuery = "SELECT inmoney FROM in_out WHERE inmoney not in ('0') and date = "
    			+ BDUtil.getDateDB(toYear, toMonth+1, toDay) + " and type not in (" 
    			+ BDConstents.INOUT_TYPE + ", " 
    			+ BDConstents.OUTIN_TYPE + ", " + fanSql
    			+ BDConstents.MYBANK_INOUT_TYPE +")"
    			+ "UNION ALL "
			    + "SELECT inmoney FROM in_out WHERE date = " + BDUtil.getDateDB(toYear, toMonth+1, toDay) 
			    + " and category in ('" 
			    + BDConstents.CATEGORY_SUSUMONEY + "')";
    	
    	sql.selectMapTile(context, sqlQuery, new ParseSelectData()
		{
			@Override
			public void resultData(Cursor c)
			{
				int count = c.getCount();
				
				int columIndex = c.getColumnIndex("inmoney");
				c.moveToFirst();
//				int money = 0;
				money = BigDecimal.ZERO;
				for (int i = 0; i < count; i++)
				{
//					money = money + Integer.parseInt(c.getString(columIndex));
					money = money.add(new BigDecimal(c.getString(columIndex).replaceAll(",", "")));
					c.moveToNext();
				}
			}
		});
		
		return money;
	}
	
	private boolean addFlag = true;
	/**
	 * 통장 디테일 테이블에서 자산 포함인지 미포함인지 값을 가져온다.
	 * @param linkid
	 * @return
	 */
	private boolean getAddBankBookFlag(int linkid)
	{
		addFlag = true;
		
		sql.selectMapTile(context, "SELECT addflag FROM bankbook_detail " +
				"WHERE linkid = " + linkid + ";", 
				new ParseSelectData()
		{
			@Override
			public void resultData(Cursor c)
			{
				int linkidIdx = c.getColumnIndex("addflag");
				
				c.moveToFirst();
				
				int flag = 0;
				
				for (int i = 0; i < c.getCount(); i++)
				{
					flag = c.getInt(linkidIdx);
					c.moveToNext();
				}
				
				if (flag == 0)
				{
					addFlag = true;
				}
				else
				{
					addFlag = false;
				}
			}
		});
		
		return addFlag;
	}
	
	/**
	 * 자산이 삭제 되면 셋팅 값도 삭제 시킨다.
	 * @param orgName
	 * @param managerKey
	 */
	public void setManagerDelete(String orgName, String managerKey)
	{
		String updateName = "자산삭제됨";
		
		SharedPreferences pref = context.getSharedPreferences("pref", Activity.MODE_PRIVATE);
		SharedPreferences.Editor editor = pref.edit();
		
		String bigMain = pref.getString(BDConstents.BIG_MAIN_SETTING, "0,1,2,3,6,4,5,9,7,8"); 
		String bigWidget = pref.getString(BDConstents.BIG_WIDGET_SETTING, "0,1,2,3");
		String smallWidget = pref.getString(BDConstents.SMALL_WIDGET_SETTING, "4,5");
		
		// 메인 셋팅
		String [] mainKey = bigMain.split(",");
		
		if (mainKey.length < 10 )
		{
			bigMain = "0,1,2,3,6,4,5,9,7,8";
			mainKey = bigMain.split(",");
		}
		
		for (int i = 0; i < mainKey.length; i++)
		{
			if (mainKey[i].equals(managerKey))
			{
				String constentName = BDConstents.BIG_MAIN_MONEYKIND1.
						substring(0, BDConstents.BIG_MAIN_MONEYKIND1.length()-1) + (i+1);
				String mainName = getName(mainKey[i], i, 1);
				
				if (mainName.equals(orgName))
				{
					editor.putString(constentName, updateName);
				}
			}
		}
		
		// 큰 위젯 셋팅
		String [] bigWidgetKey = bigWidget.split(",");
		
		if (bigWidgetKey.length < 4 )
		{
			bigWidget = "0,1,2,3";
			bigWidgetKey = bigWidget.split(",");
		}
		
		for (int i = 0; i < bigWidgetKey.length; i++)
		{
			if (bigWidgetKey[i].equals(managerKey))
			{
				String constentName = BDConstents.BIG_WIDGET_MONEYKIND1.
						substring(0, BDConstents.BIG_WIDGET_MONEYKIND1.length()-1) + (i+1);
				String mainName = getName(bigWidgetKey[i], i, 1);
				
				if (mainName.equals(orgName))
				{
					editor.putString(constentName, updateName);
				}
			}
		}
		
		// 작은 위젯 셋팅
		String [] smallWidgetKey = smallWidget.split(",");
		
		if (smallWidgetKey.length < 2 )
		{
			smallWidget = "4,5";
			smallWidgetKey = smallWidget.split(",");
		}
		
		for (int i = 0; i < smallWidgetKey.length; i++)
		{
			if (smallWidgetKey[i].equals(managerKey))
			{
				String constentName = BDConstents.SMALL_WIDGET_MONEYKIND1.
						substring(0, BDConstents.SMALL_WIDGET_MONEYKIND1.length()-1) + (i+1);
				String mainName = getName(smallWidgetKey[i], i, 1);
				
				if (mainName.equals(orgName))
				{
					editor.putString(constentName, updateName);
				}
			}
		}
		
		editor.commit();
	}
	
	/**
	 * 자산이 수정 되면 셋팅 값도 같이 수정한다.
	 * @param orgName
	 * @param updateName
	 * @param managerKey
	 */
	public void setManagerUpdate(String orgName, String updateName, String managerKey)
	{
		SharedPreferences pref = context.getSharedPreferences("pref", Activity.MODE_PRIVATE);
		SharedPreferences.Editor editor = pref.edit();
		
		String bigMain = pref.getString(BDConstents.BIG_MAIN_SETTING, "0,1,2,3,6,4,5,9,7,8"); 
		String bigWidget = pref.getString(BDConstents.BIG_WIDGET_SETTING, "0,1,2,3");
		String smallWidget = pref.getString(BDConstents.SMALL_WIDGET_SETTING, "4,5");
		
		// 메인 셋팅
		String [] mainKey = bigMain.split(",");
		
		if (mainKey.length < 10 )
		{
			bigMain = "0,1,2,3,6,4,5,9,7,8";
			mainKey = bigMain.split(",");
		}
		
		for (int i = 0; i < mainKey.length; i++)
		{
			if (mainKey[i].equals(managerKey))
			{
				String constentName = BDConstents.BIG_MAIN_MONEYKIND1.
						substring(0, BDConstents.BIG_MAIN_MONEYKIND1.length()-1) + (i+1);
				String mainName = getName(mainKey[i], i, 1);
				
				if (mainName.equals(orgName))
				{
					editor.putString(constentName, updateName);
				}
			}
		}
		
		// 큰 위젯 셋팅
		String [] bigWidgetKey = bigWidget.split(",");
		
		if (bigWidgetKey.length < 4 )
		{
			bigWidget = "0,1,2,3";
			bigWidgetKey = bigWidget.split(",");
		}
		
		for (int i = 0; i < bigWidgetKey.length; i++)
		{
			if (bigWidgetKey[i].equals(managerKey))
			{
				String constentName = BDConstents.BIG_WIDGET_MONEYKIND1.
						substring(0, BDConstents.BIG_WIDGET_MONEYKIND1.length()-1) + (i+1);
				String mainName = getName(bigWidgetKey[i], i, 1);
				
				if (mainName.equals(orgName))
				{
					editor.putString(constentName, updateName);
				}
			}
		}
		
		// 작은 위젯 셋팅
		String [] smallWidgetKey = smallWidget.split(",");
		
		if (smallWidgetKey.length < 2 )
		{
			smallWidget = "4,5";
			smallWidgetKey = smallWidget.split(",");
		}
		
		for (int i = 0; i < smallWidgetKey.length; i++)
		{
			if (smallWidgetKey[i].equals(managerKey))
			{
				String constentName = BDConstents.SMALL_WIDGET_MONEYKIND1.
						substring(0, BDConstents.SMALL_WIDGET_MONEYKIND1.length()-1) + (i+1);
				String mainName = getName(smallWidgetKey[i], i, 1);
				
				if (mainName.equals(orgName))
				{
					editor.putString(constentName, updateName);
				}
			}
		}
		
		editor.commit();
	}
	
	/**
	 * 검색할 쿼리를 반환한다.
	 * @param key 타입 번호
	 * @param number 자산위치
	 * @return
	 */
	public String getSelectQuery(String key, int number)
	{
		String query = "";
		query = "SELECT * FROM in_out WHERE date >= " + firstDate + " and date <= " + lastDate + " "; 
		
		if (key.equals(TOTAL_MONEY))
		{
			query = query + "";
		}
		else if (key.equals(BANK_MONEY))
		{
			query = query + "and type = " + BDConstents.BANK_TYPE + 
					" or " + "linktype = " + BDConstents.BANK_TYPE + "";
		}
		else if (key.equals(REAL_MONEY))
		{
			query = query + "and type = " + BDConstents.REAL_TYPE + 
					" or " + "linktype = " + BDConstents.REAL_TYPE + "";
		}
		else if (key.equals(CREDIT_MONEY))
		{
			SharedPreferences pref = context.getSharedPreferences("pref", Activity.MODE_PRIVATE);
			/** 시작일로 카드대금 가져올지 기간으로 가져올지 플래그 */
			int postCheck = pref.getInt(BDConstents.MONTH_POSTMONEY_SETTING, 0);
			
			if (postCheck == 0)
			{
				query = query + "and type = " + BDConstents.CREDIT_TYPE + 
						" or " + "linktype = " + BDConstents.CREDIT_TYPE + "";
			}
			else
			{
				query = "";
				
				getCreditResultList();
				
				for (int i = 0; i < creditResultSelectList.size(); i++)
				{
					CreditCardResultDateData data = creditResultSelectList.get(i);
					
					int startDay = Integer.parseInt(data.getStartdate());
					int endDay = Integer.parseInt(data.getEnddate());
					
					if (toDay >= startDay)
					{
						if (startDay == 1)
						{
							firstDate = BDUtil.getDateDB(toYear, toMonth+1, startDay);
							lastDate = BDUtil.getDateDB(toYear, toMonth+1, endDay);
						}
						else if (toMonth == 11)
						{
							firstDate = BDUtil.getDateDB(toYear, toMonth+1, startDay);
							lastDate = BDUtil.getDateDB(toYear+1, 1, endDay);
						}
						else
						{
							firstDate = BDUtil.getDateDB(toYear, toMonth+1, startDay);
							lastDate = BDUtil.getDateDB(toYear, toMonth+2, endDay);
						}
					}
					else
					{
						if (startDay == 1)
						{
							firstDate = BDUtil.getDateDB(toYear, toMonth, endDay);
							lastDate = BDUtil.getDateDB(toYear, toMonth, endDay);
						}
						else if (toMonth == 0)
						{
							firstDate = BDUtil.getDateDB(toYear-1, 12, startDay);
							lastDate = BDUtil.getDateDB(toYear, toMonth+1, endDay);
						}
						else
						{
							firstDate = BDUtil.getDateDB(toYear, toMonth, startDay);
							lastDate = BDUtil.getDateDB(toYear, toMonth+1, endDay);
						}
					}
					
					if (firstDate.equals(""))
					{
						continue;
					}
					
					query = query + "SELECT * FROM in_out WHERE " +
							"date >= " + firstDate + " and date <= " + lastDate + " ";
					
					query = query + "and moneykind = '" + BDUtil.getApostrophe(data.getName()) + "' ";
					
					query = query + "and type = " + BDConstents.CREDIT_TYPE + 
							" or " + "linktype = " + BDConstents.CREDIT_TYPE + "";
					
					
					if (i < creditResultSelectList.size()-1)
					{
						query = query + " UNION ALL ";
					}
				}
			}
		}
		else if (key.equals(MONTH_OUT_MONEY))
		{
			query = "SELECT * FROM in_out WHERE " +
					"inmoney in ('0') and (outmoney not in ('0')) and date >= " +
					firstDate + " and date <= " +
					lastDate + " and type not in (" + 
					BDConstents.INOUT_TYPE + ", " +
					BDConstents.OUTIN_TYPE + ", " +
					BDConstents.MYBANK_INOUT_TYPE + ") "
					+ "UNION ALL "
					+ "SELECT * FROM in_out WHERE date >= " +
					firstDate + " and date <= " +
					lastDate + " and category in ('" 
					+ BDConstents.CATEGORY_SUSUMONEY + "')";
		}
		else if (key.equals(MONTH_POST_MONEY))
		{
			SharedPreferences pref = context.getSharedPreferences("pref", Activity.MODE_PRIVATE);
			/** 시작일로 카드대금 가져올지 기간으로 가져올지 플래그 */
			int postCheck = pref.getInt(BDConstents.MONTH_POSTMONEY_SETTING, 0);
			
			if (postCheck == 0)
			{
				query = "SELECT * FROM in_out WHERE " +
						"inmoney in ('0') and (postmoney not in ('0')) and date >= " +
						firstDate + " and date <= " +
						lastDate + " and type not in (" + 
						BDConstents.INOUT_TYPE + ", " +
						BDConstents.OUTIN_TYPE + ", " +
						BDConstents.MYBANK_INOUT_TYPE + ") "
						+ "UNION ALL "
						+ "SELECT * FROM in_out WHERE date >= " +
						firstDate + " and date <= " +
						lastDate + " and category in ('" 
						+ BDConstents.CATEGORY_SUSUMONEY + "')";
			}
			else
			{
				query = "";
				
				getCreditResultList();
				
				for (int i = 0; i < creditResultSelectList.size(); i++)
				{
					CreditCardResultDateData data = creditResultSelectList.get(i);
					
					int startDay = Integer.parseInt(data.getStartdate());
					int endDay = Integer.parseInt(data.getEnddate());
					
					if (toDay >= startDay)
					{
						if (startDay == 1)
						{
							firstDate = BDUtil.getDateDB(toYear, toMonth+1, startDay);
							lastDate = BDUtil.getDateDB(toYear, toMonth+1, endDay);
						}
						else if (toMonth == 11)
						{
							firstDate = BDUtil.getDateDB(toYear, toMonth+1, startDay);
							lastDate = BDUtil.getDateDB(toYear+1, 1, endDay);
						}
						else
						{
							firstDate = BDUtil.getDateDB(toYear, toMonth+1, startDay);
							lastDate = BDUtil.getDateDB(toYear, toMonth+2, endDay);
						}
					}
					else
					{
						if (startDay == 1)
						{
							firstDate = BDUtil.getDateDB(toYear, toMonth, endDay);
							lastDate = BDUtil.getDateDB(toYear, toMonth, endDay);
						}
						else if (toMonth == 0)
						{
							firstDate = BDUtil.getDateDB(toYear-1, 12, startDay);
							lastDate = BDUtil.getDateDB(toYear, toMonth+1, endDay);
						}
						else
						{
							firstDate = BDUtil.getDateDB(toYear, toMonth, startDay);
							lastDate = BDUtil.getDateDB(toYear, toMonth+1, endDay);
						}
					}
					
					if (firstDate.equals(""))
					{
						continue;
					}
					
					query = query + "SELECT * FROM in_out WHERE " +
							"inmoney in ('0') and (postmoney not in ('0')) and date >= " +
							firstDate + " and date <= " +
							lastDate + " and type not in (" + 
							BDConstents.INOUT_TYPE + ", " +
							BDConstents.OUTIN_TYPE + ", " +
							BDConstents.MYBANK_INOUT_TYPE + ") " +
							"and moneykind = '" + BDUtil.getApostrophe(data.getName()) + "' "
							+ "UNION ALL "
							+ "SELECT * FROM in_out WHERE date >= " +
							firstDate + " and date <= " +
							lastDate + " and category in ('" 
							+ BDConstents.CATEGORY_SUSUMONEY + "') " +
							"and moneykind = '" + BDUtil.getApostrophe(data.getName()) + "'";
					
					if (i < creditResultSelectList.size()-1)
					{
						query = query + " UNION ALL ";
					}
				}
			}
		}
		else if (key.equals(MONTH_IN_MONEY))
		{
			query = "SELECT * FROM in_out WHERE inmoney not in ('0') and date >= " +
					firstDate + " and date <= " +
					lastDate + " and type not in (" + 
					BDConstents.INOUT_TYPE + ", " +
					BDConstents.OUTIN_TYPE + ", " +
					BDConstents.MYBANK_INOUT_TYPE + 
					")";
		}
		else if (key.equals(TODAY_OUT_MONEY))
		{
			query = "SELECT * FROM in_out WHERE " +
					"inmoney in ('0') and (outmoney not in ('0')) and date = " +
					BDUtil.getDateDB(toYear, toMonth+1, toDay) +
					" and type not in (" + 
					BDConstents.INOUT_TYPE + ", " +
					BDConstents.OUTIN_TYPE + ", " +
					BDConstents.MYBANK_INOUT_TYPE + ") "
					+ "UNION ALL "
					+ "SELECT * FROM in_out WHERE date = " +
					firstDate + " and date = " +
					lastDate + " and category in ('" 
					+ BDConstents.CATEGORY_SUSUMONEY + "')";
		}
		else if (key.equals(TODAY_POST_MONEY))
		{
			query = "SELECT * FROM in_out WHERE " +
					"inmoney in ('0') and (postmoney not in ('0')) and date = " +
					BDUtil.getDateDB(toYear, toMonth+1, toDay) + 
					" and type not in (" + 
					BDConstents.INOUT_TYPE + ", " +
					BDConstents.OUTIN_TYPE + ", " +
					BDConstents.MYBANK_INOUT_TYPE + ") "
					+ "UNION ALL "
					+ "SELECT * FROM in_out WHERE date = " +
					firstDate + " and date = " +
					lastDate + " and category in ('" 
					+ BDConstents.CATEGORY_SUSUMONEY + "')";
		}
		else if (key.equals(TODAY_IN_MONEY))
		{
			query = "SELECT * FROM in_out WHERE inmoney not in ('0') and date = " +
					BDUtil.getDateDB(toYear, toMonth+1, toDay) + 
					" and type not in (" + 
					BDConstents.INOUT_TYPE + ", " +
					BDConstents.OUTIN_TYPE + ", " +
					BDConstents.MYBANK_INOUT_TYPE + 
					")";
		}
		else if (key.equals(BANKBOOK) || key.equals(CHECKCARD))
		{
			String name = getMoneyKindName(number, 1);
			
			query = query + "and moneykind = '" + BDUtil.getApostrophe(name) + "'";
		}
		else if (key.equals(CREDITCARD))
		{
			SharedPreferences pref = context.getSharedPreferences("pref", Activity.MODE_PRIVATE);
			/** 시작일로 카드대금 가져올지 기간으로 가져올지 플래그 */
			int postCheck = pref.getInt(BDConstents.MONTH_POSTMONEY_SETTING, 0);
			
			if (postCheck == 0)
			{
				String name = getMoneyKindName(number, 1);
				
				query = query + "and moneykind = '" + BDUtil.getApostrophe(name) + "'";
			}
			else
			{
				query = "";
				
				getCreditResultList();
				
				for (int i = 0; i < creditResultSelectList.size(); i++)
				{
					CreditCardResultDateData data = creditResultSelectList.get(i);
					
					int startDay = Integer.parseInt(data.getStartdate());
					int endDay = Integer.parseInt(data.getEnddate());
					
					if (toDay >= startDay)
					{
						if (startDay == 1)
						{
							firstDate = BDUtil.getDateDB(toYear, toMonth+1, startDay);
							lastDate = BDUtil.getDateDB(toYear, toMonth+1, endDay);
						}
						else if (toMonth == 11)
						{
							firstDate = BDUtil.getDateDB(toYear, toMonth+1, startDay);
							lastDate = BDUtil.getDateDB(toYear+1, 1, endDay);
						}
						else
						{
							firstDate = BDUtil.getDateDB(toYear, toMonth+1, startDay);
							lastDate = BDUtil.getDateDB(toYear, toMonth+2, endDay);
						}
					}
					else
					{
						if (startDay == 1)
						{
							firstDate = BDUtil.getDateDB(toYear, toMonth, endDay);
							lastDate = BDUtil.getDateDB(toYear, toMonth, endDay);
						}
						else if (toMonth == 0)
						{
							firstDate = BDUtil.getDateDB(toYear-1, 12, startDay);
							lastDate = BDUtil.getDateDB(toYear, toMonth+1, endDay);
						}
						else
						{
							firstDate = BDUtil.getDateDB(toYear, toMonth, startDay);
							lastDate = BDUtil.getDateDB(toYear, toMonth+1, endDay);
						}
					}
					
					if (firstDate.equals(""))
					{
						continue;
					}
					
					String name = getMoneyKindName(number, 1);
					
					query = query + "SELECT * FROM in_out WHERE " +
							"date >= " + firstDate + " and date <= " + lastDate + " ";
					
					query = query + "and moneykind = '" + BDUtil.getApostrophe(name) + "'";
					
					if (i < creditResultSelectList.size()-1)
					{
						query = query + " UNION ALL ";
					}
				}
			}
		}
		else if (key.equals(SECRET))
		{
			query = "";
		}
		else if (key.equals(MONTH_MOVE_MONEY))
		{
			query = "SELECT * FROM in_out WHERE date >= " + firstDate + " and date <= " + lastDate + " "
					+ "and type in (" + 
					BDConstents.INOUT_TYPE + ", " +
					BDConstents.OUTIN_TYPE + ", " +
					BDConstents.MYBANK_INOUT_TYPE + 
					")";
		}
		else if (key.equals(TODAY_MOVE_MONEY))
		{
			query = "SELECT * FROM in_out WHERE date = " + BDUtil.getDateDB(toYear, toMonth+1, toDay) + " "
					+ "and type in (" + 
					BDConstents.INOUT_TYPE + ", " +
					BDConstents.OUTIN_TYPE + ", " +
					BDConstents.MYBANK_INOUT_TYPE + 
					")";
		}
		
		return query;
	}

	private void getCreditResultList()
	{
		creditResultSelectList = new ArrayList<CreditCardResultDateData>();
		
		sql.selectMapTile(context, "SELECT * FROM credit_card_result_date;", new ParseSelectData()
		{
			@Override
			public void resultData(Cursor c)
			{
				int idIdx = c.getColumnIndex("id");
				int nameIdx = c.getColumnIndex("name");
				int autoynIdx = c.getColumnIndex("autoyn");
				int startdateIdx = c.getColumnIndex("startdate");
				int enddateIdx = c.getColumnIndex("enddate");
				int resultdateIdx = c.getColumnIndex("resultdate");
				int resultflagIdx = c.getColumnIndex("resultflag");
				int linkidIdx = c.getColumnIndex("linkid");
				
				c.moveToFirst();
				
				for (int i = 0; i < c.getCount(); i++)
				{
					CreditCardResultDateData creditResultData = new CreditCardResultDateData();
					
					creditResultData.setId(c.getInt(idIdx) + "");
					creditResultData.setName(c.getString(nameIdx));
					creditResultData.setAutoyn(c.getInt(autoynIdx) + "");
					creditResultData.setStartdate(c.getInt(startdateIdx) + "");
					creditResultData.setEnddate(c.getInt(enddateIdx) + "");
					creditResultData.setResultdate(c.getInt(resultdateIdx) + "");
					creditResultData.setResultflag(c.getInt(resultflagIdx) + "");
					creditResultData.setLinkid(c.getInt(linkidIdx) + "");
					
					creditResultSelectList.add(creditResultData);
					
					c.moveToNext();
				}
			}
		});
	}
	
	public void setOnClick(View view, final String key, final int number)
	{
		if (key.equals(WidgetGetSqlQuery.SECRET))
		{
			view.setOnClickListener(null);
			view.setClickable(false);
		}
		else
		{
			view.setClickable(true);
			view.setOnClickListener(new OnClickListener()
			{
				@Override
				public void onClick(View v)
				{
					Intent intent = new Intent(context, SelectActivity.class);
					intent.putExtra(MainActivity.SELECT_FLAG, true);
					intent.putExtra(MainActivity.MAIN_KEY, key);
					intent.putExtra(MainActivity.MAIN_NUMBER, number);
					intent.putExtra(MainActivity.START_DATE, firstDate);
					intent.putExtra(MainActivity.END_DATE, lastDate);
					intent.putExtra(MainActivity.MAIN_TO_YEAR, toYear);
					intent.putExtra(MainActivity.MAIN_TO_MONTH, toMonth);
					intent.putExtra(MainActivity.MAIN_TO_DAY, toDay);
					context.startActivity(intent);
				}
			});
		}
		
	}
}
